#!/bin/bash

EXE=$1
OUTPUT_BASE_DIR=./920f.Comp.dir
# OUTPUT_CSV_PATH=$2
MATRIX_LIST=$2
PLATFORM=$3

THREADS_LIST=(32 64 96 128)

# 遍历不同的 OMP 线程数
for OMP_NUM in "${THREADS_LIST[@]}"; do
  OUTPUT_DIR="${OUTPUT_BASE_DIR}/threads_${OMP_NUM}"
  mkdir -p "$OUTPUT_DIR"

  echo "Running tests with OMP_NUM_THREADS=$OMP_NUM"

  # 逐行读取 matrix_list 并处理每个矩阵文件
  while IFS= read -r MATRIX_FILE; do
    # 跳过空行
    [[ -z "$MATRIX_FILE" ]] && continue

    # 检查文件是否存在
    [ -e "$MATRIX_FILE" ] || {
      echo "File not found: $MATRIX_FILE"
      continue
    }

    # 获取文件名（不带路径）
    MATRIX_NAME=$(basename "$MATRIX_FILE")
    OUTPUT_CSV_PATH="${OUTPUT_BASE_DIR}/btlu-nprocs=${OMP_NUM}-${PLATFORM}.csv"

    # 运行 EXE 并将输出重定向到对应文件
    echo "Processing: $MATRIX_FILE with OMP_NUM_THREADS=$OMP_NUM"
    echo OMP_NUM_THREADS=$OMP_NUM "$EXE" "$MATRIX_FILE" "$OUTPUT_CSV_PATH" | tee "$OUTPUT_DIR/$MATRIX_NAME.log"
    OMP_PROC_BIND=close OMP_NUM_THREADS=$OMP_NUM "$EXE" "$MATRIX_FILE" "$OUTPUT_CSV_PATH" | tee "$OUTPUT_DIR/$MATRIX_NAME.log"

  done <"$MATRIX_LIST"

  echo "Finished processing with OMP_NUM_THREADS=$OMP_NUM"
done

echo "All matrices processed for all thread configurations!"
